package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrdersProvinceSta;
import com.fau.om.util.DateUtil;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  各省份订单统计Action
 * @author hufq
 * @datetime 2014年9月28日,下午12:58:53
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class ProvinceOrdersStatisticsAction extends PublicAction implements ModelDriven<MedicalOrdersProvinceSta>{
	
	private static final long serialVersionUID = 9136009940093902556L;
	
	private MedicalOrdersProvinceSta provinceSta = new MedicalOrdersProvinceSta();

	/**
	 * @desc 各省份订单统计页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月28日,下午1:00:06
	 */
	@Action(value="province_orders_sta_init",results={@Result(name=SUCCESS,location="province_orders_sta_init.jsp")})
	public String orderView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询各省份订单统计
	 * @return null
	 * @author kangyj
	 * @datetime 2014年12月11日,下午9:33:05
	 */
	@Action(value="province_orders_sta_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getStatisticsManagerService().findProvinceStatisticsByPage(provinceSta, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出各省份订单统计
	 * @return null
	 * @author liy
	 * @datetime 2014年12月14日,下午10:21:41
	 */
	@Action(value = "province_orders_sta_expots")
	public String proExports() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"各省份订单统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsManagerService().provinceStaExport(this.provinceSta);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersProvinceSta getModel() {
		return provinceSta;
	}

}
